package com.ma4567.remotecontroller.MysqlHelper;

import com.ma4567.remotecontroller.RemoteControllerApplication;
import com.ma4567.remotecontroller.UserManager.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Date;
import java.sql.SQLException;

class RequestLog{
    public String ip;
    public int Target;
    public String operation;
    public String cookie;

    public RequestLog(String operation, String cookie, String ip, int target){
        this.ip = ip;
        this.Target = target;
        this.operation = operation;
        this.cookie = cookie;
    }

    public RequestLog(String operation, String cookie, String ip){
        this.ip = ip;
        this.Target = -1;
        this.operation = operation;
        this.cookie = cookie;
    }
}

public class TableRequestLog {
    public static void Init() throws SQLException {
        Connection conn = ConnectPool.getConnection();
        PreparedStatement stmt = conn.prepareStatement(
                "create table if not exists request_log(" +
                        "time      datetime not null key," +
                        "ip        text     not null," +
                        "cookie    text     not null," +
                        "operation text     not null," +
                        "target    int      null," +
                        "constraint request_log_user_Account_fk foreign key (target) references user (Account));");
        stmt.executeUpdate();
        conn.close();
    }

    public static void AddLog(String operation, String cookie, String ip, int target) throws SQLException {
        Connection conn = ConnectPool.getConnection();

        RequestLog newLog = new RequestLog(operation, cookie, ip, target);

        PreparedStatement stmt = conn.prepareStatement("insert ignore request_log(time,ip,cookie,operation,target) " +
                "values (?,?,?,?,?)");
        stmt.setObject(1, new java.sql.Timestamp(new java.util.Date().getTime()));
        stmt.setString(2, newLog.ip);
        stmt.setString(3, newLog.cookie);
        stmt.setString(4, newLog.operation);
        stmt.setInt(5, newLog.Target);
        stmt.executeUpdate();

        System.out.println("Request from cookie: " + newLog.cookie + ". Operation: " + newLog.operation + ". Target: " + newLog.Target);

        conn.close();
    }

    public static void AddLog(String operation, String cookie, String ip) throws SQLException {
        Connection conn = ConnectPool.getConnection();

        RequestLog newLog = new RequestLog(operation, cookie, ip);

        PreparedStatement stmt = conn.prepareStatement("insert ignore request_log(time,ip,cookie,operation) " +
                "values (?,?,?,?)");
        stmt.setObject(1, new java.sql.Timestamp(new java.util.Date().getTime()));
        stmt.setString(2, newLog.ip);
        stmt.setString(3, newLog.cookie);
        stmt.setString(4, newLog.operation);
        stmt.executeUpdate();

        System.out.println("Request from cookie: " + newLog.cookie + ". Operation: " + newLog.operation + ". Without target.");

        conn.close();
    }
}
